/*
Arm Tracker and Glove Demo by Noah Zerkin (noazark AT gmail.com) 2009
Released under GNU GPLv3.0 http://www.opensource.org/licenses/gpl-3.0.html
*/

class AtomicSphere {
  float[] coords = new float[3];
    int[] orbitVector = new int[3];
  boolean intersect = false;
  boolean grab = false;
  int objID = lastObjID + 1;
  int objRadius = 0;
  color objNormalColor = #FF0000; color objOverColor = #FF0000; color objGrabColor = #FF0000;
  boolean active = false;
  boolean activated = false;
  ArrayList satellites;
  
  
  AtomicSphere(float x, float y, float z, int radius, color normalColor, color overColor, color grabColor, int[] vector){
    coords[0] = x; coords[1] = y; coords[2] = z; objRadius = radius; objNormalColor = normalColor; objOverColor = overColor; objGrabColor = grabColor; orbitVector = vector;
    satellites = new ArrayList();
    int satCounter = 0;
    float[] createSatRot = new float[3];
    float[] createSatSpin = new float[3];
    for(int i = 1; i < 5; i += 1){
      createSatRot[0] = i;
      createSatRot[1] = i;
      createSatRot[2] = i;
      createSatSpin[0] = i + random(3,10);
      createSatSpin[1] = i + random(3,10);
      createSatSpin[2] = i + random(3,10);
    satellites.add(new Satellite(createSatRot, createSatSpin, int(objRadius/5), int(1.5*objRadius + (i*(objRadius/5)))));
    }
  }
  
  float[] getCoords(){
    return coords;
  }
  
  void drawSatellites(){
    for(int i = satellites.size()-1; i >= 0; i--) {
    Satellite thisSat = (Satellite)satellites.get(i);
    float[] satSpin = thisSat.getSpin();
    float[] satRotations = thisSat.getRotation();
  /*  for(int j = 0; j < 3; j++){
      print(leadPosition[j]); print(" - ");
    }
    */  
    satRotations[0] += satSpin[0];
    satRotations[1] += satSpin[1];
    satRotations[2] += satSpin[2];
    float[] newSatRotation = new float[3];
    for(int j = 0; j < 3; j++){
      newSatRotation[j] = satRotations[j];
    }
    Satellite newSatellite = (new Satellite(newSatRotation, satSpin, int(objRadius/5), int(1.5*objRadius + (i*(objRadius/5)))));
    satellites.set(i, newSatellite);
    newSatellite.display();
  }
  }
  
   void activate(){
    if(active && !activated && buttonC){
     activated = true;
     active = false;
    }
   if(!active && !activated && buttonC){
    activated = true;
    active = true;
   }
   if(activated && !buttonC){
     activated = false;
   }
  }
  
  boolean sphereStatus(){
    return active;
  }
  
    void update(){
     if(active){
      if(dist(obj[0][2],obj[0][3],obj[0][4],coords[0],coords[1],coords[2]) <= objRadius){

        intersect = true;
      }
      else{
        intersect = false;
        
        /*
                  coords[0] += orbitVector[0]; //coords[1] += orbitVector[1]; coords[2] += orbitVector[2];
          orbitVector[0] += ; orbitVector[2] += 1;
          */
      }
      if(!buttonC){grab=false;}
      if(intersect){
        if(buttonC){
          if(!grabbed && grabbedObjID != objID){
            grab = true;
            grabbed = true;
            grabbedObjID = objID;
          }
          else{
            if(grabbed && grabbedObjID == objID){
              grab = true;
            }
            else{
              if(!grabbed && grabbedObjID == objID){
                grab = true;
                grabbed = true;
              }
              else{
                if(grabbed && grabbedObjID != objID){
                  grab = false;
                }
              }
            }
          }
        }
        else{
          grab = false;
        }
      }
      if(!grab && grabbedObjID == objID){
        grabbed = false;
      }
      if(grab && grabbedObjID == objID){
        coords[0] = grabbedObjectX[0];
        coords[1] = grabbedObjectY[0];
        coords[2] = grabbedObjectZ[0];
        grabbedObjRadius = objRadius;
      }
      pushMatrix();
      translate(coords[0],coords[1],coords[2]);
      if(intersect){
        if(grab){
          stroke(objGrabColor);
        }
        else{
          stroke(objOverColor);
        }
      }
      else{
        stroke(objNormalColor);
      }
      noStroke();
      specular(100, 100, 100);
      shininess(.5);
      fill(objNormalColor);
      sphere(objRadius);
      drawSatellites();
      popMatrix();
    }
  
}
    }
  
  
class Satellite{
  float[] rotations = new float[3];
  float[] spinVector = new float[3];
  int radius = 0;
  int distance = 0;
  
  Satellite(float[] rotated, float[] spinny, int satRadius, int satDistance){
    radius = satRadius;
    distance = satDistance;
    rotations[0] = rotated[0];
    rotations[1] = rotated[1];
    rotations[2] = rotated[2];
    spinVector[0] = spinny[0];
    spinVector[1] = spinny[1];
    spinVector[2] = spinny[2];
  }
  
  void update(){
    rotations[0] += spinVector[0];
    rotations[1] += spinVector[1];
    rotations[2] += spinVector[2];
  }
  
  float[] getSpin(){
    return spinVector;
  }
  
  float[] getRotation(){
    return rotations;
  }
  
  void display(){
    pushMatrix();
    rotateX(radians(rotations[0]));
    rotateY(radians(rotations[1]));
    rotateZ(radians(rotations[2]));
    translate(0, distance, 0);
    sphere(radius);
 //   pointLight(51, 102, 126, radius+1, 0, 0);
    popMatrix();
  }
  
}

class Cycler{
  
  Cycler(){
    
  }
  
}
